package com.yaozheng.vocationaltraining.view.question;

import net.sf.json.JSONArray;

import org.androidannotations.annotations.CheckedChange;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

/**
 * 多选题
 *
 * @author Administrator
 */
@EViewGroup(R.layout.view_question_multiple_choice)
public class MultipleChoiceQuestionView extends BaseQuestionView {
    @ViewById
    CheckBox optionsCheck1, optionsCheck2, optionsCheck3, optionsCheck4;
    @ViewById
    View optionsImage1, optionsImage2, optionsImage3, optionsImage4;


    public MultipleChoiceQuestionView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public MultipleChoiceQuestionView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void setOptionsContent(String... contents) {
        optionsCheck1.setText(contents[0]);
        optionsCheck2.setText(contents[1]);
        optionsCheck3.setText(contents[2]);
        optionsCheck4.setText(contents[3]);

    }

    @Override
    public void setOptionsContent(JSONArray contents) {
        optionsCheck1.setText(TypeUtils.getJsonString(contents, 0));
        optionsCheck2.setText(TypeUtils.getJsonString(contents, 1));
        optionsCheck3.setText(TypeUtils.getJsonString(contents, 2));
        optionsCheck4.setText(TypeUtils.getJsonString(contents, 3));
    }

    @CheckedChange({R.id.optionsCheck1, R.id.optionsCheck2, R.id.optionsCheck3, R.id.optionsCheck4})
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {

            if (onAnswerChange != null) {
                onAnswerChange.answerChange(this);
            }
        } else {
            if (buttonView == optionsCheck1) {
                if (optionsImage1.getVisibility() == View.VISIBLE) {
                    optionsCheck1.setChecked(true);
                    return;
                }
            } else if (buttonView == optionsCheck2) {
                if (optionsImage2.getVisibility() == View.VISIBLE) {
                    optionsCheck2.setChecked(true);
                    return;
                }
            } else if (buttonView == optionsCheck3) {
                if (optionsImage3.getVisibility() == View.VISIBLE) {
                    optionsCheck3.setChecked(true);
                    return;
                }
            } else if (buttonView == optionsCheck4) {
                if (optionsImage4.getVisibility() == View.VISIBLE) {
                    optionsCheck4.setChecked(true);
                    return;
                }
            }
        }
    }

    @Override
    public String getAnswer() {
        StringBuffer answer = null;
        if (optionsCheck1.isChecked()) {
            if (answer == null) {
                answer = new StringBuffer();
            } else {
                answer.append(",");
            }
            answer.append("A");
        }

        if (optionsCheck2.isChecked()) {
            if (answer == null) {
                answer = new StringBuffer();
            } else {
                answer.append(",");
            }
            answer.append("B");
        }
        if (optionsCheck3.isChecked()) {
            if (answer == null) {
                answer = new StringBuffer();
            } else {
                answer.append(",");
            }
            answer.append("C");
        }

        if (optionsCheck4.isChecked()) {
            if (answer == null) {
                answer = new StringBuffer();
            } else {
                answer.append(",");
            }
            answer.append("D");
        }
        return answer == null ? null : answer.toString();
    }


    @Override
    public void setAnswer(String answer) {
        if (answer != null) {
            if (answer.indexOf("A") != -1) {
                optionsCheck1.setChecked(true);
            }
            if (answer.indexOf("B") != -1) {
                optionsCheck2.setChecked(true);

            }
            if (answer.indexOf("C") != -1) {
                optionsCheck3.setChecked(true);
            }
            if (answer.indexOf("D") != -1) {
                optionsCheck4.setChecked(true);

            }
        }
    }

    @Override
    public void optionsCard(JSONArray optionsCard) {
        optionsImage1.setVisibility(View.GONE);
        optionsImage2.setVisibility(View.GONE);
        optionsImage3.setVisibility(View.GONE);
        optionsImage4.setVisibility(View.GONE);
        if (optionsCard != null) {
            for (int i = 0; i < TypeUtils.getJsonArraySize(optionsCard); i++) {
                String optionCard = TypeUtils.getJsonString(optionsCard, i);
                if ("A".equals(optionCard)) {
                    optionsImage1.setVisibility(View.VISIBLE);
                    optionsCheck1.setChecked(true);

                } else if ("B".equals(optionCard)) {
                    optionsImage2.setVisibility(View.VISIBLE);
                    optionsCheck2.setChecked(true);

                } else if ("C".equals(optionCard)) {
                    optionsImage3.setVisibility(View.VISIBLE);
                    optionsCheck3.setChecked(true);

                } else if ("D".equals(optionCard)) {
                    optionsImage4.setVisibility(View.VISIBLE);
                    optionsCheck4.setChecked(true);

                }
            }
        }
    }

}
